package cn.jiguang.jgadpubadapter.adapter;

import android.app.Activity;
import android.content.Context;

import cn.jiguang.jgadpubadapter.JgPubAdInit;
import cn.jiguang.jgadpubadapter.LogUtil;
import cn.jiguang.jgssp.ad.ADJgRewardVodAd;
import cn.jiguang.jgssp.ad.data.ADJgRewardVodAdInfo;
import cn.jiguang.jgssp.ad.entity.ADJgAdSize;
import cn.jiguang.jgssp.ad.entity.ADJgExtraParams;
import cn.jiguang.jgssp.ad.error.ADJgError;
import cn.jiguang.jgssp.ad.listener.ADJgRewardVodAdListener;
import xyz.adscope.amps.ad.reward.adapter.AMPSRewardAdapter;
import xyz.adscope.amps.ad.reward.adapter.AMPSRewardVideoAdAdapterListener;
import xyz.adscope.amps.ad.splash.adapter.AMPSSplashAdAdapterListener;
import xyz.adscope.amps.common.AMPSError;
import xyz.adscope.amps.init.inter.IAMPSChannelInitCallBack;
import xyz.adscope.amps.inner.AMPSAdBiddingListener;
import xyz.adscope.amps.model.AMPSAdapterModel;
import xyz.adscope.amps.tool.util.AMPSScreenUtil;

public class JgPuAdRewardVideoAdapter extends AMPSRewardAdapter {

    ADJgRewardVodAd adJgRewardVodAd;

    @Override
    public boolean isValid() {
        if (adJgRewardVodAd != null) {
            return false;
        }
        return true;
    }

    @Override
    public void showAd(Activity activity) {
        LogUtil.d("showad");
        if (adJgRewardVodAdInfo != null) {
            adJgRewardVodAdInfo.showRewardVod((Activity) mContext);
        }
    }

    @Override
    public void startBid(Context context, AMPSAdapterModel ampsAdapterModel,
                         AMPSAdBiddingListener listener) {
        super.startBid(context, ampsAdapterModel, listener);
        initSDK();
    }

    @Override
    public void loadNetworkAd(Context context, AMPSAdapterModel ampsAdapterModel, AMPSRewardVideoAdAdapterListener listener) {
        super.loadNetworkAd(context, ampsAdapterModel, listener);
        //实现竞价逻辑时需要判断是否是bidding广告，如果是bidding广告，如果已经发起过请求直接回调广告请求成功
        if (isBidding && adJgRewardVodAd != null) {
            JgPuAdRewardVideoAdapter.this.onAdLoad();
            return;
        }
        initSDK();
    }

    @Override
    public void destroy() {
        super.destroy();
        if (adJgRewardVodAdInfo != null) {
            adJgRewardVodAdInfo = null;
        }
        if (adJgRewardVodAd != null) {
            adJgRewardVodAd = null;
        }
    }

    private void initSDK() {
        JgPubAdInit.getInstance().initSDK(mInitAdapterConfig, new IAMPSChannelInitCallBack() {
            @Override
            public void successCallBack() {
                loadAd();
            }

            @Override
            public void failCallBack(AMPSError ampsError) {
                JgPuAdRewardVideoAdapter.this.onAdFailed(ampsError.getCode(), ampsError.getMessage());
            }
        });
    }

    private ADJgRewardVodAdInfo adJgRewardVodAdInfo;

    private void loadAd() {
        ADJgExtraParams extraParams = new ADJgExtraParams.Builder()
                .adSize(new ADJgAdSize(AMPSScreenUtil.getScreenWidth(mContext), AMPSScreenUtil.getScreenHeight(mContext)))
                .build();
        adJgRewardVodAd = new ADJgRewardVodAd((Activity) mContext);
        adJgRewardVodAd.setLocalExtraParams(extraParams);
        adJgRewardVodAd.setListener(new ADJgRewardVodAdListener() {
            @Override
            public void onVideoCache(ADJgRewardVodAdInfo adJgRewardVodAdInfo) {
                LogUtil.d("onVideoCache");
            }

            @Override
            public void onVideoComplete(ADJgRewardVodAdInfo adJgRewardVodAdInfo) {
                LogUtil.d("onVideoComplete");
                JgPuAdRewardVideoAdapter.this.onVideoComplete();
            }

            @Override
            public void onVideoError(ADJgRewardVodAdInfo adJgRewardVodAdInfo, ADJgError adJgError) {
                LogUtil.d("onVideoError");
                JgPuAdRewardVideoAdapter.this.onVideoError();
            }

            @Override
            public void onReward(ADJgRewardVodAdInfo adJgRewardVodAdInfo) {
                LogUtil.d("onReward");
                JgPuAdRewardVideoAdapter.this.onRewardArrived(true,0,adJgRewardVodAdInfo.getRewardMap());
            }

            @Override
            public void onAdReceive(ADJgRewardVodAdInfo adJgRewardVodAdInfo) {
                LogUtil.d("onAdReceive");
                JgPuAdRewardVideoAdapter.this.adJgRewardVodAdInfo = adJgRewardVodAdInfo;
                if (isBidding) {
                    int mEcpm = (int) adJgRewardVodAdInfo.getECPM();
                    JgPuAdRewardVideoAdapter.this.onC2SBiddingSuccess(mEcpm);
                } else {
                    JgPuAdRewardVideoAdapter.this.onAdLoad();
                }
            }

            @Override
            public void onAdExpose(ADJgRewardVodAdInfo adJgRewardVodAdInfo) {
                JgPuAdRewardVideoAdapter.this.onAdShow();
            }

            @Override
            public void onAdClick(ADJgRewardVodAdInfo adJgRewardVodAdInfo) {
                JgPuAdRewardVideoAdapter.this.onAdClicked();
            }

            @Override
            public void onAdClose(ADJgRewardVodAdInfo adJgRewardVodAdInfo) {
                JgPuAdRewardVideoAdapter.this.onAdDismiss();
            }

            @Override
            public void onAdFailed(ADJgError adJgError) {
                JgPuAdRewardVideoAdapter.this.onAdFailed(String.valueOf(adJgError.getCode()), adJgError.getError());
            }
        });
        adJgRewardVodAd.loadAd(mSpaceId);
    }
}
